﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using filmquiz.Src.Domain;
using filmquiz.Src.DatabaseAccess;
using filmquiz.Src;

public partial class Quiz : System.Web.UI.Page
{
    private QuizHelper quizHelper = QuizHelper.getInstance();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["imageNumber"] != null)
        {
            Session["image"] = int.Parse(Request.Params["imageNumber"]);
        }
        else if (Session["image"] != null && ((int)Session["image"]) == 0)
        {
            Session["image"] = 1;
        }

        answerTrue.Visible = false;
        answerWrong.Visible = false;
        allSolved.Visible = false;
        movieTitelLabel.Visible = false;
        answerTable.Visible = true;
        notLoggedInLabel.Visible = false;
        quizImage.Visible = true;
        percentageLabel.Visible = true;
    }

    private void Page_PreRender(object sender, System.EventArgs e)
    {
        if (Session["loggedIn"] != null)
        {
            quizButtonWrapper.Visible = (Boolean)Session["loggedIn"];

            if ((Boolean)Session["LoggedIn"])
            {
                createQuizButtons();

                string imgUrl = Statics.getInstance().getImageBaseUrl() + Statics.getInstance().getCurrentRound() + "/" + (int)Session["image"] + ".jpg";
                quizImage.ImageUrl = imgUrl;
                imgLink.NavigateUrl = imgUrl;

                showPercentage();

                imageNo.Value = "ctl00_MainPlaceHolder_butt" + (int)Session["image"];

                if(((User)Session["user"]).getQuestions()[((int)Session["image"]) - 1] || quizHelper.isRoundOver())
                {
                    movieTitelLabel.Text = QuizHelper.getInstance().getAnswer((int)Session["image"]);
                    movieTitelLabel.Visible = true;

                    answerTable.Visible = false;
                }
            }
            else
            {
                quizImage.Visible = false;
                percentageLabel.Visible = false;
                answerTable.Visible = false;
                notLoggedInLabel.Visible = true;
            }
        }
        else
        {
            quizButtonWrapper.Visible = false;
            quizImage.Visible = false;
            percentageLabel.Visible = false;
        }

    }

    /// <summary>
    /// Creates the number of buttons specified in Statics.cs
    /// </summary>
    private void createQuizButtons()
    {
        int numberOfQuestions = Statics.getInstance().getNumberOfQuestions();
        User user = (User)Session["user"];

        for (int i = 1; i <= numberOfQuestions; i++)
        {
            HyperLink hyperlink = new HyperLink();
            hyperlink.Text = i + "";
            hyperlink.NavigateUrl = "Quiz.aspx?imageNumber=" + i;
            hyperlink.CssClass = "quizButton";
            hyperlink.ID = "butt" + i;

            Label imageNumberLabel = new Label();
            imageNumberLabel.Text = "" + i;
            
            Image solvedUnsolvedImage = new Image();
            if (user.getQuestions()[i - 1])
            {
                solvedUnsolvedImage.ImageUrl = "Web_Resources/images/checkmark.png";
            }
            else
            {
                solvedUnsolvedImage.ImageUrl = "Web_Resources/images/questionsMark.png";
            }

            hyperlink.Controls.Add(imageNumberLabel);
            hyperlink.Controls.Add(solvedUnsolvedImage);

            quizButtonsPlaceholder.Controls.Add(hyperlink);           
        }

    }

    protected void answerButton_Click(object sender, EventArgs e)
    {
        if (answerText.Text.Length > 0)
        {
            DbAcces dbAcces = DbAcces.getInstance();

            string answer = answerText.Text;

            answer = answer.Replace("'", "#");

            bool succes = quizHelper.checkAnswer((int)Session["image"], answer);
            User user = (User)Session["user"];

            if (succes)
            {
                user.setQuestionTrue((int)Session["image"]);
                answerTrue.Visible = true;

                if (isAllSolved())
                {
                    allSolved.Visible = true;
                    UserHelper.getInstance().playerCompleted(user.getUserID());
                }

            }
            else
            {
                QuizHelper.getInstance().insertWrongAnswer(answer, ((int)Session["image"]), ((User)Session["user"]).getUserID());
                answerWrong.Visible = true;
            }

            answerText.Text = ""; 
        }
    }

    protected void Quiz_Button_Click(object sender, EventArgs e)
    {
        Session["image"] = System.Convert.ToInt32(((Button)sender).Text);
    }

    private void showPercentage()
    {
        String text = Math.Round((StatsHelper.getInstance().getPercentageForQuestion((int)Session["image"])), 0).ToString();
        percentageLabel.Text = text + " procent af de aktive brugere har gættet dette billede.";
    }

    private bool isAllSolved()
    {
        bool allSolved = true;
        foreach (bool question in ((User)Session["user"]).getQuestions())
        {
            if (!question)
            {
                allSolved = false;
                break;
            }
        }
        return allSolved;
    }
}
